using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Reflection;
using Sonofagun.Properties;

namespace Sonofagun
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private List<Frame> leftResults = new List<Frame>();
        private Bitmap sonarImage;

        private void MenuItemClicked(object sender, EventArgs args)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            PixelFormat f = (PixelFormat)Enum.Parse(typeof(PixelFormat), item.Text);
            format = f;
            pictureBox1.Image = null;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            FieldInfo[] t = typeof(PixelFormat).GetFields();

            foreach (FieldInfo fi in t)
            {
                if (fi.FieldType == typeof(PixelFormat))
                {
                    ToolStripMenuItem item = pixelFormatToolStripMenuItem.DropDownItems.Add(fi.Name, null, new EventHandler(MenuItemClicked)) as ToolStripMenuItem;
                    item.Checked = (format.ToString() == fi.Name);
                    item.CheckOnClick = true;

                }
            }
            

        }

        [DebuggerStepThrough()]
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            //Graphics g = this.CreateGraphics();
            //g.Clear(Color.Gainsboro);

            ////int i = 0;
            ////foreach (Frame f in results)
            ////{
            ////    g.DrawImage(f.Image, 0,i);
            ////    i++;
            ////}

            //Image b = sonarImage.GetThumbnailImage(sonarImage.Width / 100, sonarImage.Height / 100, null, IntPtr.Zero);
            //g.DrawImage(sonarImage, 0, 0);

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                pictureBox1.Image = null;
                toolStripProgressBar1.Value = 0;

                Stream stream = openFileDialog.OpenFile();
                
                Thread worker = new Thread(new ParameterizedThreadStart(LoadFile));
                
                worker.Start(stream);
            }
        }

        private delegate void SetProgressDelegate(int value);

        private void SetProgress(int value)
        {
            toolStripProgressBar1.Value = value;
        }

        private delegate void InitProgressDelegate(bool visible, int max);

        private void InitProgress(bool visible, int max)
        {
            toolStripProgressBar1.Maximum = max;
            toolStripProgressBar1.Visible = visible;
            this.Cursor = visible ? Cursors.WaitCursor : Cursors.Default;
            this.Enabled = !visible;
        }

        private delegate void LoadCompleteDelegate();

        private void LoadComplete()
        {
            pictureBox1.Image = null;
            pictureBox1.Image = sonarImage;
        }

        private PixelFormat format
        {
            get
            {
                return Settings.Default.PixelFormat;
            }
            set
            {
                Settings.Default["PixelFormat"] = value;
                Settings.Default.Save();
            }

        }
         
        private void LoadFile(object parameter)
        {
            Stream stream = parameter as Stream;

            //toolStripProgressBar1.Maximum = (int)(stream.Length / 100);

            this.Invoke(new InitProgressDelegate(InitProgress), true, (int)(stream.Length / 100));

            using (SonReader reader = new SonReader(stream))
            {
                reader.Format = format;

                while (reader.Position < reader.Length)
                {
                    leftResults.Add(reader.ReadLine());
                    //      toolStripProgressBar1.Value = (int)(stream.Position / 100);
                    this.Invoke(new SetProgressDelegate(SetProgress), (int)(stream.Position / 100));
                }

                this.Invoke(new InitProgressDelegate(InitProgress), false, (int)(stream.Length / 100));
            }

            sonarImage = new Bitmap(leftResults[0].Data.Length, leftResults.Count, format);

            for (int i = 0; i < leftResults.Count; i++)
            {

                Rectangle rect = new Rectangle(new Point(0, i),new Size(sonarImage.Width, 1));

                BitmapData bitmapData = sonarImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, format);
                IntPtr ptr = bitmapData.Scan0;
                Marshal.Copy(leftResults[i].Data, 0, ptr, leftResults[i].Data.Length);

                sonarImage.UnlockBits(bitmapData); 
            }

            //Graphics g = Graphics.FromImage(sonarImage);
            //g.Clear(Color.Black);

            //for (int i = 0; i < leftResults.Count; i++)
            //{
            //    g.DrawImage(leftResults[i].Image, 0, i);
            //}

            this.Invoke(new LoadCompleteDelegate(LoadComplete));

           
        }
    }
}